package com.duke.shaking.profile;

import java.io.InputStream;
import java.net.URL;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;

public class URL2DrawableTask extends AsyncTask<String, Void, Drawable>{
	private OnCompletionListener mListener;

	@Override
	protected Drawable doInBackground(String... params) {
		return LoadImageFromWebOperations(params[0]);
	}

	public Drawable LoadImageFromWebOperations(String url) {
		try {
			InputStream is = (InputStream)new URL(url).getContent();
			Drawable d = Drawable.createFromStream(is, "src name");
			return d;
		} catch (Exception e) {
			System.out.println("Exc=" + e);
			return null;
		}
	}

	public void setOnCompletionListener(OnCompletionListener onCompletionListener) {
		this.mListener = onCompletionListener;
	}

	public interface OnCompletionListener {
		void onTaskComplete(Drawable result);
	}


	@Override
	protected void onPostExecute(Drawable result) { 
		super.onPostExecute(result);
		if(result == null || mListener == null) return;
		mListener.onTaskComplete(result);
	} 
}
